(function () {


    "use strict";

    var URL1 = {
        servreq: {
            getServReq: "/rest/servreq/getServReq?servReq_Id={0}",
            getServReqExternal: "/rest/servreq/getServReqExternal?servReq_Id={0}&external_Token={1}"
        },
        customcarecase: {
            updateCustomCareCaseExternal: "/rest/customcarecase/updateCustomCareCaseExternal",
            updateCustomCareCase: "/rest/customcarecase/updateCustomCareCase?"
        }
    };
    app.controller('EvaluationDataTableController', EvaluationDataTableController);
    function EvaluationDataTableController($scope, RESTService, AuthService, $filter, $compile, $state, $modal, $rootScope, $translate, $stateParams) {
        // define value
        $scope.servReq_Id = $stateParams.servReq_Id;
        if ($scope.servReq_Id == null && $scope.items != null) {
            $scope.servReq_Id = $scope.items;
        }

        $scope.external_token = $stateParams.external_token;

        if ($stateParams.external_token) {
            $scope.showCloseBtn = false;
        } else {
            $scope.showCloseBtn = true;
        }

        $scope.init = function () {
            $scope.pain_Level = "1";
            $scope.compare_Previous = "yes";
            $scope.service_Satis = "VeryGood";
            $scope.feedback = "";
            $scope.finished = false; // the flag of finished of evaluation

            if ($scope.servReq_Id != null && $scope.servReq_Id != "") {
                if ($scope.external_token != null && $scope.external_token != "") {
                    RESTService.getHttp(AuthService.getRsContext() + URL1.servreq.getServReqExternal.format($scope.servReq_Id, $scope.external_token)).then(
                        function (response) {
                            if (response.code != null && response.code == 0) {
                                $scope.servReq = response.data[0];

                                $scope.pain_Level = $scope.servReq._customCareCase.pain_Level;
                                $scope.compare_Previous = $scope.servReq._customCareCase.compare_Previous;
                                $scope.service_Satis = $scope.servReq._customCareCase.service_Satis;
                                $scope.feedback = $scope.servReq._customCareCase.feedback;
                            }
                        });
                }
                else {
                    RESTService.getResponse(URL1.servreq.getServReq.format($scope.servReq_Id), true).then(
                        function (response) {
                            if (response.code != null && response.code == 0) {
                                $scope.servReq = response.data[0];

                                $scope.pain_Level = $scope.servReq._customCareCase.pain_Level;
                                $scope.compare_Previous = $scope.servReq._customCareCase.compare_Previous;
                                $scope.service_Satis = $scope.servReq._customCareCase.service_Satis;
                                $scope.feedback = $scope.servReq._customCareCase.feedback;
                            }
                        });
                }
            }

        };

        // initial to read data and vars
        $scope.init();

        $scope.setPain_Level = function (index) {
            $scope.pain_Level = $scope.Pain_Levels[index].key;
        }
        $scope.setCompare_Previous = function (index) {
            $scope.compare_Previous = index;
        }
        $scope.setService_Satis = function (index) {
            $scope.service_Satis = $scope.service_Satises[index].key;
        }

        $scope.finishEditEvaluation = function () {
            var param = {
                servReq_Id: $scope.servReq_Id,
                pain_Level: $scope.pain_Level,
                compare_Previous: $scope.compare_Previous,
                service_Satis: $scope.service_Satis,
                feedback: $scope.feedback,
                external_Token: $scope.external_token
            };
            // alert($.param(param));
            if ($scope.external_token != null) {
                // without access_token, from Wechat
                RESTService.getHttp(AuthService.getRsContext() + URL1.customcarecase.updateCustomCareCaseExternal + "?" + $.param(param))
                    .then(function (response) {
                        if (response != null && response.code == 0) {
                            // set isFinish flag to true of servReq._customCareCase
                            $scope.finished = true;
                        }
                    });
            }
            else {
                // with access_token, from model window
                RESTService.postResponse(URL1.customcarecase.updateCustomCareCase, param).then(function (response) {
                    if (undefined !== response && response.length > 0) {
                        // set isFinish flag to true of servReq._customCareCase
                        // item.isFinish = true;
                        $scope.cancel();
                    }
                });
            }
        };
    }
})();